package cn.itsource.org.controller;

import cn.hutool.core.util.StrUtil;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description:
 * @Author: ZHl
 * @Date: 2022-09-11 14:42
 */
@RestController
@RequestMapping("/department")
@Api(tags = "部门控制器")  //swagger API
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;



    /**
     * 查询所有部门
     * @return  AjaxResult
     */
    @GetMapping("/tree")
    public AjaxResult getDeptTree(){
       return AjaxResult.createSuccess( departmentService.getDeptTree());
    }

    /**
     * 查询所有部门
     * @return  AjaxResult
     */
    @GetMapping
    public AjaxResult list(){
       return AjaxResult.createSuccess( departmentService.findAll());
    }

    /**
     * 查询所有上级部门
     * @return  AjaxResult
     */
    @GetMapping("/findAllDept")
    public AjaxResult findAllDept(){
        List<Department> departments = departmentService.findAll();
        return AjaxResult.createSuccess(departments);
    }

    /**
     * 查询所有部门(分页)
     * @return  AjaxResult
     */
    @PostMapping("/pageList")
    public PageList findPage(@RequestBody DepartmentQuery departmentQuery){
        PageList pageResult = departmentService.pageList(departmentQuery);
        return pageResult;
    }

    /**
     * 新增/修改部门
     * @return AjaxResult
     */
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@Valid  @RequestBody  Department department){

            //验证方式一：
//            if(StrUtil.isBlank(department.getName())){
//                return AjaxResult.createError("请输入姓名");
//            }
//            if(StrUtil.isBlank(department.getSn())){
//                return AjaxResult.createError("请输入编号");
//            }

            if(department.getId() == null){
                departmentService.save(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.createSuccess();

    }

    /**
     * 删除部门
     * @return AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable  Long id){
        departmentService.delete(id);
        return AjaxResult.createSuccess();
    }

    /**
     * 删除部门
     * @return AjaxResult
     */
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody DepartmentQuery departmentQuery){
        departmentService.deleteBatch(departmentQuery);
        return AjaxResult.createSuccess();
    }

}
